home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0079_Iterate Fields of a Table.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-24  |  2.1 KB  |  85 lines

  1. {
  2. There are a number of reasons why a program might need to query the
  3. structure of a table used in the application. One reason is a prelude to
  4. creating TField components at run-time that represent the fields in the
  5. table. The information gleaned from the structure of the table form the
  6. basis of the TField components to be created.
  7.  
  8. The example below demonstrates how to iterate through the fields available
  9. in a TTable or TQuery. The example extracts information about the available
  10. fields and displays the information in a TListBox, but the same methodology
  11. can be used to provide information necessary for the dynamic building of
  12. TField descendants. The example uses a TTable as the data set, but a TQuery
  13. can be used in the same manner as both TTable and TQuery components incorp-
  14. orate the Field-Defs property the same way.
  15. }
  16.  
  17. procedure TForm1.Button1Click(Sender: TObject);
  18. var
  19.   i: Integer;
  20.   F: TFieldDef;
  21.   D: String;
  22. begin
  23.   Table1.Active := True;
  24.   ListBox1.Items.Clear;
  25.   with Table1 do begin
  26.     for i := 0 to FieldDefs.Count - 1 do begin
  27.       F := FieldDefs.Items[i];
  28.       case F.DataType of
  29.         ftUnknown: D := 'Unknown';
  30.         ftString: D := 'String';
  31.         ftSmallint: D := 'SmallInt';
  32.         ftInteger: D := 'Integer';
  33.         ftWord: D := 'Word';
  34.         ftBoolean: D := 'Boolean';
  35.         ftFloat: D := 'Float';
  36.         ftCurrency: D := 'Currency';
  37.         ftBCD: D := 'BCD';
  38.         ftDate: D := 'Date';
  39.         ftTime: D := 'Time';
  40.         ftDateTime: D := 'DateTime';
  41.         ftBytes: D := 'Bytes';
  42.         ftVarBytes: D := '';
  43.         ftBlob: D := 'BLOB';
  44.         ftMemo: D := 'Memo';
  45.         ftGraphic: D := 'Graphic';
  46.       else
  47.         D := '';
  48.       end;
  49.       ListBox1.Items.Add(F.Name + ', ' + D);
  50.     end;
  51.   end;
  52.   Table1.Active := False;
  53. end;
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. DISCLAIMER: You have the right to use this technical information
  82. subject to the terms of the No-Nonsense License Statement that
  83. you received with the Borland product to which this information
  84. pertains.
  85.